/**
 * 养老金计算相关工具函数
 */

/**
 * 计算基础养老金
 * @param {number} localAverageSalary - 当地上年度在岗职工月平均工资
 * @param {number} personalIndexedSalary - 本人指数化月平均缴费工资
 * @param {number} contributionYears - 缴费年限
 * @returns {number} 基础养老金
 */
export const calculateBasicPension = (localAverageSalary, personalIndexedSalary, contributionYears) => {
  return (localAverageSalary + personalIndexedSalary) / 2 * contributionYears * 0.01;
};

/**
 * 计算本人指数化月平均缴费工资
 * @param {number} localAverageSalary - 当地上年度在岗职工月平均工资
 * @param {number} avgContributionIndex - 本人平均缴费工资指数
 * @returns {number} 本人指数化月平均缴费工资
 */
export const calculatePersonalIndexedSalary = (localAverageSalary, avgContributionIndex) => {
  return localAverageSalary * avgContributionIndex;
};

/**
 * 计算本人平均缴费工资指数
 * @param {Array<number>} contributionIndices - 历年缴费指数
 * @param {number} contributionYears - 缴费年限
 * @returns {number} 本人平均缴费工资指数
 */
export const calculateAvgContributionIndex = (contributionIndices, contributionYears) => {
  const sum = contributionIndices.reduce((acc, index) => acc + index, 0);
  return sum / contributionYears;
};

/**
 * 计算在职员工的单年缴费指数
 * @param {number} currentSalary - 当前税前工资
 * @param {number} localAverageSalary - 当地上年度在岗职工月平均工资
 * @returns {number} 缴费指数
 */
export const calculateContributionIndex = (currentSalary, localAverageSalary) => {
  let index = currentSalary / localAverageSalary;
  // 指数下限为60%，上限为300%
  return Math.max(0.6, Math.min(3, index));
};

/**
 * 计算个人账户养老金
 * @param {number} personalAccountTotal - 个人账户累计存储额
 * @param {number} payoutMonths - 计发月数
 * @returns {number} 个人账户养老金
 */
export const calculatePersonalAccountPension = (personalAccountTotal, payoutMonths) => {
  return personalAccountTotal / payoutMonths;
};

/**
 * 获取计发月数
 * @param {number} retirementAge - 退休年龄
 * @returns {number} 计发月数
 */
export const getPayoutMonths = (retirementAge) => {
  if (retirementAge === 60) return 139;
  if (retirementAge === 55) return 170;
  if (retirementAge === 50) return 195;
  return 139; // 默认60岁
};

/**
 * 计算月基本养老金
 * @param {number} basicPension - 基础养老金
 * @param {number} personalAccountPension - 个人账户养老金
 * @returns {number} 月基本养老金
 */
export const calculateMonthlyPension = (basicPension, personalAccountPension) => {
  return basicPension + personalAccountPension;
};

/**
 * 计算在职人员的个人账户累计存储额
 * @param {number} monthlySalary - 月工资
 * @param {number} personalContributionRate - 个人缴费比例（默认8%）
 * @param {number} contributionYears - 缴费年限
 * @returns {number} 个人账户累计存储额
 */
export const calculateEmployeePAT = (monthlySalary, personalContributionRate = 0.08, contributionYears) => {
  return monthlySalary * personalContributionRate * 12 * contributionYears;
};

/**
 * 计算灵活就业人员的个人账户累计存储额
 * @param {number} localAverageSalary - 当地上年度在岗职工月平均工资
 * @param {number} baseRate - 基准比例（60%、70%、80%、90%、100%、150%、200%、250%、300%）
 * @param {number} contributionYears - 缴费年限
 * @returns {number} 个人账户累计存储额
 */
export const calculateFreelancerPAT = (localAverageSalary, baseRate, contributionYears) => {
  const totalContribution = localAverageSalary * baseRate * 0.2 * 12 * contributionYears;
  return totalContribution * 0.08 / 0.2; // 个人账户只有8%
};

/**
 * 计算个人缴费总金额 - 在职人员
 * @param {number} monthlySalary - 月工资
 * @param {number} personalContributionRate - 个人缴费比例（默认8%）
 * @param {number} contributionYears - 缴费年限
 * @returns {number} 个人缴费总金额
 */
export const calculateEmployeeTotalContribution = (monthlySalary, personalContributionRate = 0.08, contributionYears) => {
  return monthlySalary * personalContributionRate * 12 * contributionYears;
};

/**
 * 计算个人缴费总金额 - 灵活就业人员
 * @param {number} localAverageSalary - 当地上年度在岗职工月平均工资
 * @param {number} baseRate - 基准比例
 * @param {number} contributionYears - 缴费年限
 * @returns {number} 个人缴费总金额
 */
export const calculateFreelancerTotalContribution = (localAverageSalary, baseRate, contributionYears) => {
  return localAverageSalary * baseRate * 0.2 * 12 * contributionYears;
};

/**
 * 计算养老金领取总金额
 * @param {number} monthlyPension - 月基本养老金
 * @param {number} years - 领取年数
 * @returns {number} 养老金领取总金额
 */
export const calculateTotalPensionReceived = (monthlyPension, years) => {
  return monthlyPension * 12 * years;
};

/**
 * 找到养老金收支平衡点（收回本金的年数）
 * @param {number} totalContribution - 个人缴费总金额
 * @param {number} monthlyPension - 月基本养老金
 * @returns {number} 平衡点（年）
 */
export const findBreakEvenPoint = (totalContribution, monthlyPension) => {
  if (monthlyPension === 0) return Infinity;
  return totalContribution / (monthlyPension * 12);
}; 